/*
 * com.attilax.lbs.ZipXCsvGUI.java  
 *
 * Created on __DATE__, __TIME__
 */

package com.attilax.lbs;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.attilax.Closure;
import com.attilax.core;
import com.attilax.io.filex;
import com.attilax.ui.MsgBox;

/**
 * 
 * @author __USER__
 */
public class ZipXCsvGUI extends javax.swing.JFrame {

	private boolean msgStopFlag;
	private boolean curLineStopFlag;

	/** Creates new form ZipXCsvGUI */
	public ZipXCsvGUI() {

		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jButton1 = new javax.swing.JButton();
		jTextField1_out = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		jLabel3 = new javax.swing.JLabel();
		jTextField2_idx = new javax.swing.JTextField();
		jTextField3_file = new javax.swing.JTextField();
		jButton2 = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		jTextField1_startline = new javax.swing.JTextField();
		jButton3 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jButton1.setText("START\u5f00\u59cb");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jTextField1_out.setText("c:\\rzt.csv");

		jLabel2.setText("\u8f93\u51fa\u7ed3\u679c\u6587\u4ef6");

		jTextArea1.setColumns(20);
		jTextArea1.setRows(5);
		jScrollPane1.setViewportView(jTextArea1);

		jLabel3.setText("\u5730\u5740\u5217\u7d22\u5f15");

		jTextField2_idx.setText("3");
//		jTextField2_idx.addActionListener(new java.awt.event.ActionListener() {
//			public void actionPerformed(java.awt.event.ActionEvent evt) {
//				jTextField2_idxActionPerformed(evt);
//			}
//		});

		jTextField3_file.setText("c:\\a.csv");

		jButton2.setText("\u9009\u62e9\u6587\u4ef6");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jLabel1.setText("\u8d77\u59cb\u884c");

		jTextField1_startline.setText("1");

		jButton3.setText("\u6682\u505c/\u505c\u6b62");
		jButton3.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton3ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(34, 34,
																		34)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						jLabel3)
																				.addComponent(
																						jButton2)
																				.addComponent(
																						jLabel2))
																.addGap(18, 18,
																		18)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jTextField3_file,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						589,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										jTextField2_idx,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										39,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addGap(215,
																										215,
																										215)
																								.addComponent(
																										jLabel1)
																								.addGap(18,
																										18,
																										18)
																								.addComponent(
																										jTextField1_startline,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										154,
																										javax.swing.GroupLayout.PREFERRED_SIZE))
																				.addComponent(
																						jTextField1_out,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						524,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						jScrollPane1,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						617,
																						javax.swing.GroupLayout.PREFERRED_SIZE)))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(123,
																		123,
																		123)
																.addComponent(
																		jButton1,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		155,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addGap(39, 39,
																		39)
																.addComponent(
																		jButton3,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		155,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(27, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(35, 35, 35)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jButton2)
												.addComponent(
														jTextField3_file,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel3)
												.addComponent(
														jTextField2_idx,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														jTextField1_startline,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel1))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel2)
												.addComponent(
														jTextField1_out,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														jButton1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														31,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														jButton3,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														31,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										336, Short.MAX_VALUE).addContainerGap()));

		jTextField3_file.getAccessibleContext().setAccessibleName("file1");

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	/**
	 * stop
		@author attilax 老哇的爪子
		@since   obs i_e_8
	 */
	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
	 c.stop(new Closure () {

		@Override
		public Object execute(Object arg0) throws Exception {
			stopCurLineNMsgTimer();
			MsgBox.show("手动终止");
			return null;
		}
	});
	}

		/**
		@author attilax 老哇的爪子
		@since   obs i_n_41
		 
		 */
	protected void stopCurLineNMsgTimer() {
		 msgStopFlag=true;
		 curLineStopFlag=true;
		
	}

	private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		jFileChooser1 = new javax.swing.JFileChooser();
		jFileChooser1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				File f = jFileChooser1.getSelectedFile();
				jTextArea1.setText(f.getAbsolutePath());
				jTextField3_file.setText(f.getAbsolutePath());
				// jtex
			}
		});
		jFileChooser1.showOpenDialog(this);
	}

	private static ExecutorService es = Executors.newFixedThreadPool(10);
	private static ExecutorService es_single = Executors
			.newSingleThreadExecutor();
	private static ExecutorService es_single4curline = Executors
			.newSingleThreadExecutor();
	javax.swing.JFileChooser jFileChooser1;
	final ZipXCsv2 c = new ZipXCsv2();

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		 msgStopFlag=false;
		 curLineStopFlag=false;
	//	c.pause(null);
	//	c.stop(null);
		// String rzt="c:\\"
		core.execMeth_Ays(new Runnable() {

			@Override
			public void run() {
				try {
					c.addIndex=Integer.parseInt(jTextField2_idx.getText());
					c.tagFile=jTextField1_out.getText();
					c.souFile=jTextField3_file.getText();
					c.startline=Integer.parseInt(jTextField1_startline.getText());
					 c.start("");
				} catch (isIngEx e) {
					jTextArea1.append("任务正在运行中。。。");
					MsgBox.show("任务正在运行中。。。");
				}

				catch (Exception e) {
					e.printStackTrace();
					jTextArea1.append(e.getMessage());
					filex.saveLog(e, "c:\\e");

				}

				//c.geneTest(500);

			}
		}, "--");
		//msg collect process 
		es_single.execute(new Runnable() {

			@Override
			public void run() {
				while (true) {
					// msgStopFlag=true;
					
					if(msgStopFlag)
					{
						jTextArea1.append("\r\n 获取信息timer stop");
						break;
					}
					try {
						//	final int n = i;
						core.sleep(300);
						String v = null;
						try {
							v = c.rztQueue.take();
						} catch (InterruptedException e) {

							e.printStackTrace();
						}
						jTextArea1.append("\r\n" + v + " ！");
						//	jTextArea1.paintImmediately(jTextArea1.getBounds());
						jTextArea1.setCaretPosition(jTextArea1.getText()
								.length());// 滚动到底端
					} catch (Exception e) {
						e.printStackTrace();
					}

				}

			}
		});

		es_single4curline.execute(new Runnable() {

			@Override
			public void run() {
				
				while (true) {
					if( curLineStopFlag)
					{jTextArea1.append("\r\n 获取行数 timer stop");
						break;
					}
					core.sleep_safe(1000);
					jTextField1_startline.setText(String.valueOf(c.nowLine));
				}

			}
		});

		// es.execute(new Runnable() {
		//
		// @Override
		// public void run() {
		//
		// }
		// });

	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new ZipXCsvGUI().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea jTextArea1;
	private javax.swing.JTextField jTextField1_out;
	private javax.swing.JTextField jTextField1_startline;
	private javax.swing.JTextField jTextField2_idx;
	private javax.swing.JTextField jTextField3_file;
	// End of variables declaration//GEN-END:variables

}